package zy.sats.zyai.config;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.*;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import zy.sats.zyai.advisor.ChatTypeHistoryAdvisor;
import zy.sats.zyai.advisor.ChatTypeTitleAdvisor;
import zy.sats.zyai.constant.SystemPrompt;
import zy.sats.zyai.enums.ChatType;
import zy.sats.zyai.service.ChatTypeHistoryService;

/**
 * @Description: TODO
 * @Author: sats@jz
 * @Date: 2025/8/6 22:36
 **/
@Configuration
public class MyChatClientConfiguration {
    @Bean
    public ChatClient lxChatClient(ChatModel dashscopeChatModel) {
        return ChatClient.builder(dashscopeChatModel).defaultSystem(SystemPrompt.LX_PROMPT)
                .build();
    }
    @Bean
     public ChatClient loveChatClient(ChatModel dashscopeChatModel, ChatMemory chatMemory,
                                      ChatTypeHistoryService chatTypeHistoryService, @Qualifier("titleChatClient")ChatClient titleChatClient) {

        return  ChatClient.builder(dashscopeChatModel)
                .defaultSystem(SystemPrompt.SYSTEM_PROMPT)
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        ChatTypeHistoryAdvisor.builder(chatTypeHistoryService).type(ChatType.ASSISTANT.getCode())
                                .order(998).build(),
                        ChatTypeTitleAdvisor.builder(chatTypeHistoryService).type(ChatType.ASSISTANT.getCode())
                                .chatClient(titleChatClient).chatMemory(chatMemory).order(999).build(),
                        MessageChatMemoryAdvisor.builder(chatMemory).order(1000).build()
                )
                .build();
    }
    @Bean
    public ChatMemory chatMemory(ChatMemoryRepository chatMemoryRepository) {
        return MessageWindowChatMemory.builder()
                .chatMemoryRepository(chatMemoryRepository)
                .maxMessages(20)
                .build();
    }

    @Bean
    public ChatClient titleChatClient(ChatModel dashscopeChatModel) {
        return ChatClient
                .builder(dashscopeChatModel)
                .defaultAdvisors(
                        new SimpleLoggerAdvisor()
                )
                .build();
    }
}
